﻿using System;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.CampaignBehaviors;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.CampaignSystem.Encounters;
using TaleWorlds.CampaignSystem.GameMenus;
using TaleWorlds.CampaignSystem.GameState;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.Core;
using TaleWorlds.SaveSystem;

namespace TaleWorlds.CampaignSystem
{
	// Token: 0x02000060 RID: 96
	public class PlayerCaptivity
	{
		// Token: 0x06000D17 RID: 3351 RVA: 0x00042829 File Offset: 0x00040A29
		internal static void AutoGeneratedStaticCollectObjectsPlayerCaptivity(object o, List<object> collectedObjects)
		{
			((PlayerCaptivity)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
		}

		// Token: 0x06000D18 RID: 3352 RVA: 0x00042837 File Offset: 0x00040A37
		protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
		{
			CampaignTime.AutoGeneratedStaticCollectObjectsCampaignTime(this._captivityStartTime, collectedObjects);
			collectedObjects.Add(this._captorParty);
			CampaignTime.AutoGeneratedStaticCollectObjectsCampaignTime(this._lastCheckTime, collectedObjects);
		}

		// Token: 0x06000D19 RID: 3353 RVA: 0x00042867 File Offset: 0x00040A67
		internal static object AutoGeneratedGetMemberValueCountOfOffers(object o)
		{
			return ((PlayerCaptivity)o).CountOfOffers;
		}

		// Token: 0x06000D1A RID: 3354 RVA: 0x00042879 File Offset: 0x00040A79
		internal static object AutoGeneratedGetMemberValueCurrentRansomAmount(object o)
		{
			return ((PlayerCaptivity)o).CurrentRansomAmount;
		}

		// Token: 0x06000D1B RID: 3355 RVA: 0x0004288B File Offset: 0x00040A8B
		internal static object AutoGeneratedGetMemberValue_captivityStartTime(object o)
		{
			return ((PlayerCaptivity)o)._captivityStartTime;
		}

		// Token: 0x06000D1C RID: 3356 RVA: 0x0004289D File Offset: 0x00040A9D
		internal static object AutoGeneratedGetMemberValue_captorParty(object o)
		{
			return ((PlayerCaptivity)o)._captorParty;
		}

		// Token: 0x06000D1D RID: 3357 RVA: 0x000428AA File Offset: 0x00040AAA
		internal static object AutoGeneratedGetMemberValue_randomNumber(object o)
		{
			return ((PlayerCaptivity)o)._randomNumber;
		}

		// Token: 0x06000D1E RID: 3358 RVA: 0x000428BC File Offset: 0x00040ABC
		internal static object AutoGeneratedGetMemberValue_lastCheckTime(object o)
		{
			return ((PlayerCaptivity)o)._lastCheckTime;
		}

		// Token: 0x1700033B RID: 827
		// (get) Token: 0x06000D1F RID: 3359 RVA: 0x000428CE File Offset: 0x00040ACE
		// (set) Token: 0x06000D20 RID: 3360 RVA: 0x000428E0 File Offset: 0x00040AE0
		public static PartyBase CaptorParty
		{
			get
			{
				return Campaign.Current.PlayerCaptivity._captorParty;
			}
			set
			{
				if (value != null)
				{
					Campaign.Current.PlayerCaptivity._captorParty.PrisonRoster.RemoveTroop(Hero.MainHero.CharacterObject, 1, default(UniqueTroopDescriptor), 0);
				}
				Campaign.Current.PlayerCaptivity._captorParty = value;
				if (value != null)
				{
					Campaign.Current.PlayerCaptivity._captorParty.AddPrisoner(Hero.MainHero.CharacterObject, 1);
					if ((Game.Current.GameStateManager.ActiveState as MapState).AtMenu)
					{
						GameMenu.SwitchToMenu(PlayerCaptivity.CaptorParty.IsSettlement ? "settlement_wait" : "prisoner_wait");
						return;
					}
					GameMenu.ActivateGameMenu(PlayerCaptivity.CaptorParty.IsSettlement ? "settlement_wait" : "prisoner_wait");
				}
			}
		}

		// Token: 0x1700033C RID: 828
		// (get) Token: 0x06000D21 RID: 3361 RVA: 0x000429A7 File Offset: 0x00040BA7
		private ICaptivityCampaignBehavior CaptivityCampaignBehavior
		{
			get
			{
				if (this._captivityCampaignBehavior == null)
				{
					this._captivityCampaignBehavior = Campaign.Current.GetCampaignBehavior<ICaptivityCampaignBehavior>();
				}
				return this._captivityCampaignBehavior;
			}
		}

		// Token: 0x1700033D RID: 829
		// (get) Token: 0x06000D22 RID: 3362 RVA: 0x000429C7 File Offset: 0x00040BC7
		// (set) Token: 0x06000D23 RID: 3363 RVA: 0x000429D8 File Offset: 0x00040BD8
		public static float RandomNumber
		{
			get
			{
				return Campaign.Current.PlayerCaptivity._randomNumber;
			}
			set
			{
				Campaign.Current.PlayerCaptivity._randomNumber = value;
			}
		}

		// Token: 0x1700033E RID: 830
		// (get) Token: 0x06000D24 RID: 3364 RVA: 0x000429EA File Offset: 0x00040BEA
		public static bool IsCaptive
		{
			get
			{
				return Campaign.Current.PlayerCaptivity._captorParty != null;
			}
		}

		// Token: 0x1700033F RID: 831
		// (get) Token: 0x06000D25 RID: 3365 RVA: 0x00042A00 File Offset: 0x00040C00
		public static int CaptiveTimeInDays
		{
			get
			{
				return (int)(CampaignTime.Now - PlayerCaptivity.CaptivityStartTime).ToDays;
			}
		}

		// Token: 0x17000340 RID: 832
		// (get) Token: 0x06000D26 RID: 3366 RVA: 0x00042A25 File Offset: 0x00040C25
		public static CampaignTime CaptivityStartTime
		{
			get
			{
				return Campaign.Current.PlayerCaptivity._captivityStartTime;
			}
		}

		// Token: 0x17000341 RID: 833
		// (get) Token: 0x06000D27 RID: 3367 RVA: 0x00042A36 File Offset: 0x00040C36
		// (set) Token: 0x06000D28 RID: 3368 RVA: 0x00042A47 File Offset: 0x00040C47
		public static CampaignTime LastCheckTime
		{
			get
			{
				return Campaign.Current.PlayerCaptivity._lastCheckTime;
			}
			set
			{
				Campaign.Current.PlayerCaptivity._lastCheckTime = value;
			}
		}

		// Token: 0x06000D29 RID: 3369 RVA: 0x00042A59 File Offset: 0x00040C59
		public static void StartCaptivity(PartyBase captorParty)
		{
			Campaign.Current.PlayerCaptivity.StartCaptivityInternal(captorParty);
			PlayerCaptivity.RandomNumber = MBRandom.RandomFloat;
		}

		// Token: 0x06000D2A RID: 3370 RVA: 0x00042A75 File Offset: 0x00040C75
		public static void OnPlayerCharacterChanged()
		{
			if (Hero.MainHero.IsPrisoner)
			{
				Campaign.Current.PlayerCaptivity.StartCaptivityInternal(Hero.MainHero.PartyBelongedToAsPrisoner);
				PlayerCaptivity.RandomNumber = MBRandom.RandomFloat;
				PlayerCaptivity.CaptorParty = Hero.MainHero.PartyBelongedToAsPrisoner;
			}
		}

		// Token: 0x06000D2B RID: 3371 RVA: 0x00042AB5 File Offset: 0x00040CB5
		public void SetRansomAmount()
		{
			this.CurrentRansomAmount = this.GetPlayerRansomValue();
		}

		// Token: 0x06000D2C RID: 3372 RVA: 0x00042AC4 File Offset: 0x00040CC4
		private int GetPlayerRansomValue()
		{
			return (int)((MBRandom.RandomFloat * 0.5f + 0.5f) * ((float)Hero.MainHero.Gold * 0.05f + 300f) * (float)(Hero.MainHero.PartyBelongedToAsPrisoner.IsSettlement ? (Hero.MainHero.PartyBelongedToAsPrisoner.Settlement.MapFaction.IsKingdomFaction ? 4 : 2) : 1) * (float)((Hero.MainHero.PartyBelongedToAsPrisoner.IsMobile && Hero.MainHero.PartyBelongedToAsPrisoner.MobileParty.IsLordParty) ? 2 : 1) * (Hero.MainHero.GetPerkValue(DefaultPerks.Trade.ManOfMeans) ? (1f + DefaultPerks.Trade.ManOfMeans.SecondaryBonus) : 1f));
		}

		// Token: 0x06000D2D RID: 3373 RVA: 0x00042B88 File Offset: 0x00040D88
		private void StartCaptivityInternal(PartyBase captorParty)
		{
			this._captivityStartTime = CampaignTime.Now;
			this._lastCheckTime = CampaignTime.Now;
			if (PlayerEncounter.Current != null)
			{
				PlayerEncounter.LeaveEncounter = true;
			}
			if (MobileParty.MainParty.CurrentSettlement != null)
			{
				LeaveSettlementAction.ApplyForParty(MobileParty.MainParty);
			}
			MobileParty.MainParty.IsActive = false;
			PartyBase.MainParty.UpdateVisibilityAndInspected(0f);
			this._captorParty = captorParty;
			this._captorParty.SetAsCameraFollowParty();
			this._captorParty.UpdateVisibilityAndInspected(0f);
			if (MobileParty.MainParty.Army != null)
			{
				if (MobileParty.MainParty.Army.LeaderParty == MobileParty.MainParty)
				{
					DisbandArmyAction.ApplyByPlayerTakenPrisoner(MobileParty.MainParty.Army);
				}
				MobileParty.MainParty.Army = null;
			}
		}

		// Token: 0x06000D2E RID: 3374 RVA: 0x00042C48 File Offset: 0x00040E48
		private void EndCaptivityInternal()
		{
			if (Hero.MainHero.IsAlive)
			{
				PartyBase.MainParty.AddElementToMemberRoster(CharacterObject.PlayerCharacter, 1, true);
				MobileParty.MainParty.ChangePartyLeader(Hero.MainHero);
			}
			if (Hero.MainHero.CurrentSettlement != null)
			{
				if (PlayerEncounter.Current != null)
				{
					PlayerEncounter.LeaveSettlement();
				}
				else if (Hero.MainHero.IsAlive)
				{
					LeaveSettlementAction.ApplyForParty(MobileParty.MainParty);
				}
				else
				{
					LeaveSettlementAction.ApplyForCharacterOnly(Hero.MainHero);
				}
			}
			if (PlayerEncounter.Current != null)
			{
				PlayerEncounter.Finish(true);
			}
			else if (Campaign.Current.CurrentMenuContext != null)
			{
				GameMenu.ExitToLast();
			}
			if (this._captorParty.IsActive)
			{
				this._captorParty.PrisonRoster.RemoveTroop(Hero.MainHero.CharacterObject, 1, default(UniqueTroopDescriptor), 0);
			}
			if (Hero.MainHero.IsAlive)
			{
				Hero.MainHero.ChangeState(Hero.CharacterStates.Active);
			}
			if (Hero.MainHero.IsAlive)
			{
				MobileParty.MainParty.IsActive = true;
				PartyBase.MainParty.SetAsCameraFollowParty();
				MobileParty.MainParty.Ai.SetMoveModeHold();
				SkillLevelingManager.OnMainHeroReleasedFromCaptivity(PlayerCaptivity.CaptivityStartTime.ElapsedHoursUntilNow);
				PartyBase.MainParty.UpdateVisibilityAndInspected(0f);
			}
			CampaignEventDispatcher.Instance.OnHeroPrisonerReleased(Hero.MainHero, this._captorParty, this._captorParty.MapFaction, EndCaptivityDetail.ReleasedAfterEscape);
			this._captorParty = null;
			this.CountOfOffers = 0;
			this.CurrentRansomAmount = 0;
		}

		// Token: 0x06000D2F RID: 3375 RVA: 0x00042DB0 File Offset: 0x00040FB0
		public static void EndCaptivity()
		{
			if (Hero.MainHero.IsAlive)
			{
				if (Hero.MainHero.IsWounded)
				{
					Hero.MainHero.HitPoints = 20;
				}
				if (Hero.MainHero.PartyBelongedToAsPrisoner != null && Hero.MainHero.PartyBelongedToAsPrisoner.IsMobile)
				{
					Hero.MainHero.PartyBelongedToAsPrisoner.MobileParty.Ai.SetDoNotAttackMainParty(12);
				}
				PlayerEncounter.ProtectPlayerSide(4f);
			}
			Campaign.Current.PlayerCaptivity.EndCaptivityInternal();
		}

		// Token: 0x06000D30 RID: 3376 RVA: 0x00042E34 File Offset: 0x00041034
		internal void Update(float dt)
		{
			MapState mapState = Game.Current.GameStateManager.ActiveState as MapState;
			if (PlayerCaptivity.IsCaptive && (dt > 0f || (mapState != null && !mapState.AtMenu)))
			{
				if (this._captorParty.IsMobile && this._captorParty.MobileParty.IsActive)
				{
					PartyBase.MainParty.MobileParty.Position2D = this._captorParty.MobileParty.Position2D;
				}
				else if (this._captorParty.IsSettlement)
				{
					PartyBase.MainParty.MobileParty.Position2D = this._captorParty.Settlement.GatePosition;
				}
				if (mapState != null && !mapState.AtMenu)
				{
					GameMenu.ActivateGameMenu(PlayerCaptivity.CaptorParty.IsSettlement ? "settlement_wait" : "prisoner_wait");
				}
				if (PlayerCaptivity.IsCaptive)
				{
					ICaptivityCampaignBehavior captivityCampaignBehavior = this.CaptivityCampaignBehavior;
					if (captivityCampaignBehavior == null)
					{
						return;
					}
					captivityCampaignBehavior.CheckCaptivityChange(dt);
				}
			}
		}

		// Token: 0x040003CB RID: 971
		[SaveableField(0)]
		private CampaignTime _captivityStartTime;

		// Token: 0x040003CC RID: 972
		[SaveableField(1)]
		private PartyBase _captorParty;

		// Token: 0x040003CD RID: 973
		[SaveableField(2)]
		public int CountOfOffers;

		// Token: 0x040003CE RID: 974
		[SaveableField(3)]
		public int CurrentRansomAmount;

		// Token: 0x040003CF RID: 975
		private ICaptivityCampaignBehavior _captivityCampaignBehavior;

		// Token: 0x040003D0 RID: 976
		[SaveableField(4)]
		private float _randomNumber;

		// Token: 0x040003D1 RID: 977
		[SaveableField(5)]
		private CampaignTime _lastCheckTime;
	}
}
